<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("./_inc/attributeFunction.php");
require_once("../_inc/_mbAppDomValue.php");

$errMsg = "";
$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$dataArea = "";
$sCondDataArea = "";
if(isset($_REQUEST["da"])){
	$dataArea = $_REQUEST["da"];
	if(!isset($aDataArea[$_REQUEST["da"]])){
		$errMsg .= _t("user disabled")." \"".$_REQUEST["da"]."\"";
	}
}else{
	$errMsg .= _t("da missing");
}

$entity = "";
if(isset($_REQUEST["ent"]))
	$entity = $_REQUEST["ent"];
if(isset($_REQUEST["ent"]))
	$entity = $_REQUEST["ent"];
if($entity != "")	{
	$sSQL = "SELECT a.*, d.type, d.length AS d_length, d.decimals AS d_decimals" .
			" FROM attribute a" .
			" LEFT JOIN domain d ON d.domain = a.domain " .
			" WHERE a.entity='$entity' ORDER BY a.sequence, a.attribute";
	$query = $objSDAO->query($sSQL);
	$row = $objSDAO->fetch();
}else{
	$errMsg .= _t("entity missing");
}

//output HTML
$aOpz = array("kMenu"=>'dam.ent');
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
echo getEntityHeader($entity);

$aCmd = array(
					"ent"=>array("pag"=>"../enty/entityView.php?da=$dataArea&ent=$entity",
										"title"=>_t("relationships"),
										"text"=>_t("relationships"),
										"perm"=>"R","align"=>"L"),
					"ins"=>array("pag"=>"attributeIns.php?da=$dataArea&ent=$entity",
										"title"=>_t("add new attribute"),
										"text"=>_t("new attribute"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>"",
										"text"=>$objUser->getHistoryText(-1),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	
//output dei dati
if($errMsg == ""){
	$numElem = 0;
	echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr><td class=\"titl01\" colspan=\"9\">List of attributes</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"tabh01\" width=\"6%\" align='right'>seq.</td>" .
			"<td class=\"tabh01\" width=\"2%\" align='center'>PK</td>" .
			"<td class=\"tabh01\" width=\"2%\" align='center'>I</td>" .
			"<td class=\"tabh01\" width=\"15%\">"._t("Attribute")."</td>" .
			"<td class=\"tabh01\" width=\"30%\">"._t("Description")."</td>" .
			"<td class=\"tabh01\" width=\"10%\">"._t("Domain")."</td>" .
			"<td class=\"tabh01\" width=\"15%\">"._t("Format.")."</td>" .
			"<td class=\"tabh01\" width=\"30%\">"._t("Default")."</td>" .
		"</tr>\n");
	while($row){
		if($objSDAO->error == ""){
			$numElem ++;
			$format = '???';
			if ($row['d_length'] == 0) {
				$length = $row['length'];
				$decimals = $row['decimals'];
			} else {
				$length = $row['d_length'];
				$decimals = $row['d_decimals'];
			}
			switch ($row["type"]) {
				case 'integer' :
				case 'date' :
				case 'date-time' :
				case 'text' :
				case 'long-text' :
				case 'blob' :
				case 'boolean' :
					$format = $row["type"];
					break;
				case 'string' :
				case 'variable-string' :
					$format = $row["type"]."(".$length.")";
					break;
				case 'number' :
				default :
					$format = $row["type"]."(".$length.",".$decimals.")";
					break;
			}
			echo("<tr>" .
					"<td class=\"text01\" align='right'>".$row["sequence"]."</td>" .
					"<td class=\"text01\" align='center'>".(($row["primary_key"]!='N')?'*':'&nbsp;')."</td>" .
					"<td class=\"text01\" align='center'>".(($row["indexed"]=='Y')?'*':'&nbsp;')."</td>" .
					"<td class=\"text01\"><a class=\"text01\" ".goToPag("attributeView.php?da=$dataArea&ent=".$row["entity"]."&att=".$row["attribute"]."&xsn=".XSN)." title=\"Visualizza l'attribute\">".$row["attribute"]."</a></td>" .
					"<td class=\"text01\">".sxHE($row["description"])."</td>" .
					"<td class=\"text01\">".$row["domain"]."</td>" .
					"<td class=\"text01\">".$format."</td>" .
					"<td class=\"text01\">".
					(($row["primary_key"]=='X')?"(auto-increment)":sxHE($row["default_value"]))."</td>" .
				"</tr>\n");
		}else{
			echo("<tr>" .
					"<td class=\"text01\" colspan=\"9\">".$objSDAO->error."</td>" .
				"</tr>\n");
		}
		$row = $objSDAO->fetch();
	}
	if($numElem == 0)
		echo("<tr>" .
				"<td class=\"text01\" colspan=\"9\">"._t("no attributes defined")."</td>".
			"</tr>\n");
	echo("</table>\n");
}
echo(commandsHTML($aCmd));
echo(footerHTML());
